/**********************************************************************
 *@Project： cloudDemo
 *@File: SystemPortalApplication
 *@Date: 2017/4/13
 *@Copyright (C) 2013-2017 http://www.posun.cn. All Rights Reserved.
 ***********************************************************************
 *注意： 本内容仅限于深圳市普盛实业有限公司内部使用，禁止外泄
 **********************************************************************
 */
package com.posun.edu.system;

import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory;
import com.posun.cloud.tracing.enums.LogType;
import com.posun.edu.system.config.ShiroProperties;
import com.posun.edu.system.ext.DemoInitListener;
import com.posun.framework.log.AresLog;
import com.posun.framework.log.LogBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PreDestroy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

/**
 * <p>
 * TODO<br>
 *
 * @author YL
 * @version 1.0
 * @createTime 2017/4/13 17:38
 * @ChangeLog
 */
@SpringBootApplication
@ImportResource("classpath:spring/spring-config.xml")
public class SystemPortalApplication extends SpringBootServletInitializer {

    private Logger logger = LoggerFactory.getLogger(SystemPortalApplication.class);

    public static void main(String[] args) {
        AresLog.info(new LogBuilder(DemoInitListener.class, LogType.TRACE, "服务DemoPortal正在准备启动..."));
        SpringApplication.run(SystemPortalApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SystemPortalApplication.class);
    }

    @Bean
    public CountDownLatch closeLatch() {
        return new CountDownLatch(1);
    }

    @PreDestroy
    public void shutdown(){
        logger.info("destroy dubbo service client ....");
        try {
            AbstractRegistryFactory.destroyAll();
            ProtocolConfig.destroyAll();
        } catch (Exception e) {
            logger.error("",e);
        }
        logger.info("destroy dubbo NettyClient ....");
        try {
            final Class<?> clazz = Class.forName("com.alibaba.dubbo.remoting.transport.netty4.NettyClient");
            Field field = clazz.getDeclaredField("channelFactory");
            field.setAccessible(true);
            Object channelFactory = field.get(null);
            Method method = channelFactory.getClass().getMethod("releaseExternalResources");
            method.invoke(channelFactory);
        } catch (Exception e){
            logger.error("",e);
        }
        try {
            Thread.sleep(1000);
        }catch (Exception e){
            logger.error("",e);
        }
    }

}
